package com.future.controller;

import com.future.enums.StatusCode;
import com.future.po.User;
import com.future.service.AdminService;
import com.future.vo.QueryInfoVo;
import com.future.vo.ResponseResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author zlb
 * @date 2022/4/18 19:28
 */
@Api(tags = "用户的API接口")
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @ApiOperation(value = "获取指定的用户", notes = "根据url中的用户id获取指定用户信息")
    @GetMapping("/{userId}")
    public ResponseResultVo getUserById(@PathVariable Integer userId) {
        User user = adminService.getById(userId);
        return ResponseResultVo.success(user);
    }

    @ApiOperation(value = "获取用户列表", notes = "获取所有用户列表")
    @PostMapping("getUserList")
    public ResponseResultVo getUserList(@RequestBody QueryInfoVo queryInfo) throws IOException {

        List<User> users = adminService.findUsers(queryInfo);
        return ResponseResultVo.success(users);
    }

    /**
     * 获取用户个人全部信息
     * @param userId
     * @return
     */
    @GetMapping("/getUserInfo")
    public ResponseResultVo getInfo(@RequestParam String userId){
        if (userId == null || userId.isEmpty()){
            return ResponseResultVo.failure(StatusCode.NOT_FOUNT,"未找到用户信息！");
        }
        User result = adminService.getById(userId);
        result.setPassword("**********");
        result.setHeadPath(result.getHeadPath().replace("C:/images/user/head/","/photo/user/avatar/"));
        return ResponseResultVo.success(result);
    }
}
